package leetcode.hot100;

import java.util.Arrays;
import java.util.HashMap;

public class number560 {
    public static void main(String[] args) {
        System.out.println(subarraySum(new int[]{1,1,1},2));
    }

    public static int subarraySum(int[] nums, int k) {
        int n=0;
        HashMap<Integer,Integer> map =new HashMap<>();
        int sum=0;
        map.put(0,1);
        for (int num : nums) {
            sum += num;
            if (map.containsKey(sum - k)) {
                n += map.get(sum - k);
            }
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
        return n;
    }
}
